﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseHelper;
using System.Data;
using System.Windows.Forms;

namespace UI.Online
{
    public class CommonUseMethods
    {
        public CommonUseMethods()
        { 
        }

        public void LoadProjectList(ref ComboBox cbb)
        {
            DBHelper dh = new DBHelper();
            dh.Query = "SELECT ProjectName FROM Projects";
            DataTable dt = dh.Fill();
            cbb.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                string name = dr["ProjectName"].ToString();
                cbb.Items.Add(name);
            }
        }

        public int FindProjectId(string name)
        {
            DBHelper dh = new DBHelper();
            dh.Query = "SELECT ProjectID FROM Projects WHERE ProjectName = '" + name + "'";
            DataTable dt = dh.Fill();
            return (int)dt.Rows[0]["ProjectID"];
        }

        public int FindPipelineId(string name)
        {
            DBHelper dh = new DBHelper();
            dh.Query = "SELECT PipelineID FROM Pipelines WHERE PipelineName = '" + name + "'";
            DataTable dt = dh.Fill();
            return (int)dt.Rows[0]["PipelineID"];
        }

        public void LoadProjectList(ref ListView lv)
        {
            DBHelper dh = new DBHelper();
            dh.Query = "SELECT * FROM Projects";
            DataTable dt = dh.Fill();
            lv.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                string name = dr["ProjectName"].ToString();
                string location = dr["Location"].ToString();
                string summary = dr["ProjectSummary"].ToString();
                ListViewItem lvi = new ListViewItem(name);
                lvi.SubItems.Add(location);
                lvi.SubItems.Add(summary);
                lv.Items.Add(lvi);
            }
        }

        public void LoadPipelineList(ref ListView lv, int projectId)
        {
            DBHelper dh = new DBHelper();
            dh.Query = "SELECT * FROM Pipelines WHERE ProjectID = " + projectId;
            DataTable dt = dh.Fill();
            lv.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                string name = dr["PipelineName"].ToString();
                string summary = dr["PipelineSummary"].ToString();
                ListViewItem lvi = new ListViewItem(name);
                lvi.SubItems.Add(summary);
                lv.Items.Add(lvi);
            }
        }

        public void LoadRecordList(ref ListView lv, int pipelineId)
        {
            DBHelper dh = new DBHelper();
            dh.Query = "SELECT * FROM Records WHERE PipelineID = " + pipelineId;
            DataTable dt = dh.Fill();
            lv.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                string timeStart = dr["TimeStart"].ToString();
                string timeEnd = dr["TimeEnd"].ToString();
                string kpStart = dr["KPStart"].ToString();
                string kpEnd = dr["KPEnd"].ToString();
                ListViewItem lvi = new ListViewItem(timeStart);
                lvi.SubItems.Add(timeEnd);
                lvi.SubItems.Add(kpStart);
                lvi.SubItems.Add(kpEnd);
                lv.Items.Add(lvi);
            }
        }
    }
}
